<template>
  <div>
    <f-form ref='form' :action='api.bps' :form-key='this.formItem.policySaleManId' @success='handleCancel'>
      <f-form-item label='部门' type='select' name='deptId' required :showSearch='true' :data='api.hyorder.listByCompanyId' label-key='deptName' value-key='deptId' :value.sync='formItem.deptId' :params='queryParam' @change='deptChange' />
      <f-form-item label='业务员' placeholder='请选择业务员' name='saleManNames' @click='openSaleManModal(1)' :readOnly='true' :required='true' :value.sync='formItem.saleManName' :trigger-change='true' v-if='formItem.deptId'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择业务员'>
          <a-icon type='user' style='cursor:pointer' @click='openSaleManModal(1)' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='结算方式' type='radio' name='settleAmountType' dataType='number' required :data='api.busCommon.settleAmountType' :value.sync='formItem.settleAmountType' />
<!--      <f-form-item label='结算价取值' type='radio' name='priceType' :required='true' dataType='number' :data='api.busCommon.priceType' :value.sync='formItem.priceType' />-->
      <f-form-item label='结算单价' name='saleManPrice'  type='number' :precision='3' data-type='number' required :value='formItem.saleManPrice' />
      <f-form-item label='结算标准' name='saleManRate' v-if='formItem.settleAmountType==1' tooltip='格式用小数表示,示例值0.18' type='number' :precision='3' data-type='number'  :value='formItem.saleManRate' />
      <f-form-item label='结算公式' placeholder='请选择结算公式' name='saleManSettleFormula' :readOnly='true' :required='true' :value.sync='formItem.saleManSettleFormula' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择公式'>
          <a-icon type='calculator' style='cursor:pointer' @click='openFormulaModal' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='开始日期' name='beginDate' type='date' required :value='formItem.beginDate' />
      <f-form-item label='结束日期' name='endDate' type='date' :value='formItem.endDate' />
    
      <f-form-item label='提成单价' extra='设置后将额外生成提成金额'  v-if='formItem.settleAmountType==1' name='rebateAmount' type='number' :precision='3' data-type='number' :value='formItem.rebateAmount' />
      <f-form-item label='提成公式' placeholder='请选择提成公式'  v-if='formItem.settleAmountType==1' name='rebateSettleFormula' :value.sync='formItem.rebateSettleFormula' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择公式'>
          <a-icon type='calculator' style='cursor:pointer' @click='openFormulaModal2' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='结算规则' type='radio' name='settleRule' dataType='number' :data='api.busCommon.settleRuleType' :value.sync='formItem.settleRule' />
      <f-form-item label='是否包票' type='radio' name='baopiao' dataType='number' :data='api.busCommon.baopiaoType' :value.sync='formItem.baopiao' />
      <f-form-item label='计算税率差' type='radio' name='hasTaxRateDiff' dataType='number' :data='api.busCommon.baopiaoType' :value.sync='formItem.hasTaxRateDiff' />
      <f-form-item label='备注' name='remark' :value='formItem.remark' />
    </f-form>
    <select-formula-modal ref='formulaModal' @onSelectChange='onSelectFormulaChange'></select-formula-modal>
    <select-formula-modal ref='formulaModal2' @onSelectChange='onSelectFormulaChange2'></select-formula-modal>
    <select-sale-man-modal ref='saleManModal' :multiple='true' @onSelectChange='onSelectSaleManChange' :selectType='2'></select-sale-man-modal>
  </div>
</template>
<script>
import api from "../../../../js/api"
import SelectFormulaModal from "../../../../components/select_formula_modal"
import SelectSaleManModal from "@/views/module/haiying/components/select_saleman_modal"

export default {
  name: "policySaleManForm",
  props: {
    policyDrugId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      refs: this.$refs,
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      formModal: false,
      autoLoad: false,
      type: 1,
      queryParam: {
        isFlp: 1
      },
      fileIds: [],//上传文件ID
      loaded: false,//是否加载完成
      modalVisible: false,
      params1: {
        formulaType: 6
      },
      loadSaleManParams: {
        deptId: null
      },
      bodyStyle: {
        height: "530px",
        overflow: "hidden",
        overflowY: "scroll"
      },
      formItem: this.getFormItem()
    }
  },
  computed:{
   
  },
  methods: {
    getFormItem() {
      return {
        policySaleManId: "",
        deptId: null,
        saleManId: null,
        saleManPrice: null,
        saleManRate: null,
        settleRule: 1,
        settleAmountType: 1,
        hasTaxRateDiff: 1,
        baopiao: 1,
        saleManSettleFormula: "",
        saleManIds: "",
        rebateAmount: null,
        beginDate: null,
        endDate: null,
        remark: ""
      }
    },
    /**
     * 选择业务员
     */
    openSaleManModal(type) {
      this.type = type
      this.$refs["saleManModal"].open({
        deptId: this.formItem.deptId,
        selectType: 2
      })
    },
    /**
     * 业务员选择回调
     */
    onSelectSaleManChange(selectedRowKeys, selectedRows) {
      let ids = []
      let names = []
      selectedRowKeys.forEach((item) => {
        ids.push(item)
      })
      selectedRows.forEach((item) => {
        names.push(item.saleManName)
      })
      if (this.type == 1) {
        this.$refs.form.getForm().setFieldsValue({ "saleManNames": names.join(",") })
        this.formItem.saleManName = names[0]
        this.formItem.saleManId = selectedRowKeys[0]
        this.formItem.saleManIds = ids.join(",")
        this.formItem.saleManNames = names.join(",")
      }
      if (this.type == 2) {
        this.$refs.form.getForm().setFieldsValue({ "saleManNameE": names[0] })
        this.formItem.saleManNameE = names[0]
        this.formItem.saleManIdE = selectedRowKeys[0]
      }
      this.$forceUpdate()
    },

    setData(data) {
      this.isEdit = false
      if (data && data.policySaleManId) {
        this.isEdit = true
        this.formItem = data
        this.loadSaleManParams.deptId = data.deptId
        this.autoLoad = true
      } else {
        this.formItem = this.getFormItem()
        this.autoLoad = false
      }
      this.modalVisible = true
    },
    handleCancel() {
      this.modalVisible = false
    },
    /**
     * 选择公式
     */
    openFormulaModal() {
      this.$refs["formulaModal"].open({
        formulaType: 3
      })
    },
    /**
     * 选择公式
     */
    openFormulaModal2() {
      this.$refs["formulaModal2"].open({
        formulaType: 3
      })
    },
    /**
     * 公式选择回调
     */
    onSelectFormulaChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "saleManSettleFormula": selectedRows[0].formulaContent })
      this.formItem.saleManSettleFormula = selectedRows[0].formulaContent
    },
    /**
     * 公式选择回调
     */
    onSelectFormulaChange2(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "rebateSettleFormula": selectedRows[0].formulaContent })
      this.formItem.rebateSettleFormula = selectedRows[0].formulaContent
    },
    deptChange(value) {
      this.loadSaleManParams.deptId = value
      this.$refs.saleMan.initData(this.loadSaleManParams)
      //清除已选择销售
      this.$refs.form.resetField("saleManId")
    },
    //提交表单
    handleSubmit(callback) {
      this.$refs.form.submit({
        policyDrugId: this.policyDrugId,
        saleManId: this.formItem.saleManId,
        saleManIdE: this.formItem.saleManIdE,
        saleManName: this.formItem.saleManName,
        saleManIds: this.formItem.saleManIds,
        saleManNames: this.formItem.saleManNames
      }, () => {
        callback();
      })
    }
  },
  created() {
  },
  components: { SelectSaleManModal, SelectFormulaModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
